#include<bits/stdc++.h>
using namespace std;
const int NN=1e7+4;
int a[NN],last[NN],ne[NN],n;
double p[NN];
bool check(double mid)
{
	p[ne[0]]=0;
	for(int i=ne[0];i;i=ne[i])
	{
		double res=1-p[i];
		if(a[i]>=2)
			res-=mid;
		if(res<0)
			return false;
		p[ne[i]]=max(0.0,mid*2-res);
	}
	return true;
}
int main()
{
	freopen("guess.in","r",stdin);
	freopen("guess.out","w",stdout);
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int a0,A,B,C,M;
		scanf("%d%d%d%d%d%d",&n,&a0,&A,&B,&C,&M);
		for(int i=0;i<=n;i++)
			a[i]=last[i]=ne[i]=0;
		for(int i=1;i<=n;i++)
		{
			a0=(1ll*a0*a0%M*A%M+1ll*a0*B%M+C)%M+1;
			a[a0]++;
		}
		for(int i=1,j=0;i<=n;i++)
			if(a[i])
			{
				ne[j]=i;
				last[i]=j;
				j=i;
			}
		double l=0,r=1;
		while(r-l>1e-7)
		{
			double mid=l+(r-l)/2;
			if(check(mid))
				l=mid;
			else
				r=mid;
		}
		printf("%.6lf 0\n",l);
	}
	return 0;
}
